package com.gewu.ordlelist.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.app.ActivityOptionsCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.gewu.ordlelist.R;
import com.gewu.ordlelist.base.BaseActivity;
import com.gewu.ordlelist.bean.ImageBean;
import com.gewu.ordlelist.database.dbbean.ByCar;
import com.gewu.ordlelist.database.dbdao.ByCarDao;
import com.gewu.ordlelist.utils.jocUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class search_activity extends BaseActivity {


    private Toolbar toolbar;
    private android.widget.TextView text;
    private android.widget.EditText sousuo;
    private androidx.recyclerview.widget.RecyclerView rec;

    @Override
    public int FindLayout() {
        return R.layout.activity_search_activity;
    }

    @Override
    public Toolbar getToolBar() {
        return toolbar;
    }

    @Override
    public boolean isShowBackToolbar() {
        return true;
    }

    @Override
    public String setToolbarText() {
        return "搜索";
    }

    @Override
    public void initview() {
        toolbar = findViewById(R.id.toolbar);
        text = findViewById(R.id.text);
        sousuo = findViewById(R.id.sousuo);
        rec = findViewById(R.id.rec);
        toolbar.setTitle(setToolbarText());

        sousuo.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_DONE) {

                    rec.setLayoutManager(new LinearLayoutManager(search_activity.this,LinearLayoutManager.VERTICAL,false));
                    rec.setAdapter(new RecyclerView.Adapter() {

                        @NonNull
                        @Override
                        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                            View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.four_item, parent, false);
                            return new searchs(inflate);
                        }

                        @Override
                        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
                            RoundedCorners roundedCorners = new RoundedCorners(50);
                            RequestOptions options = RequestOptions.bitmapTransform(roundedCorners);
                            int i = new Random().nextInt(6);
                            Glide.with(holder.itemView).load(jocUtils.getimgs()[i]).apply(options).into(((searchs) holder).image);
                            ((searchs) holder).text.setText(sousuo.getText().toString() + "\t价格：" + new Random().nextInt(100));
                            ((searchs) holder).text2.setText(R.string.text);
                            ((searchs) holder).ok.setVisibility(View.GONE);
                            ((searchs) holder).image.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    Intent intent = new Intent(holder.itemView.getContext(), moreActivity.class);
                                    intent.putExtra("index",position);
                                    intent.putExtra("name",((searchs) holder).text.getText());
                                    startActivity(intent);
                                }
                            });

                        }

                        @Override
                        public int getItemCount() {
                            return 1;
                        }
                    });


                }
                return false;
            }
        });

    }

    private class searchs extends RecyclerView.ViewHolder {

        private TextView text2;
        private TextView text;
        private ImageView image;
        private TextView ok;

        public searchs(View inflate) {
            super(inflate);
            image = inflate.findViewById(R.id.image);
            text = inflate.findViewById(R.id.text);
            text2 = inflate.findViewById(R.id.text2);
            ok = inflate.findViewById(R.id.ok);
        }
    }




}